import os
import subprocess

def normalize_audio(input_path):
    if os.path.isdir(input_path):
        # 遍历目录处理所有音频文件
        for root, dirs, files in os.walk(input_path):
            for file in files:
                if file.endswith(('.mp3')):
                    file_path = os.path.join(root, file)
                    print(f"Processing {file_path}")
                    print_loudness(file_path)  # 输出当前响度
    elif os.path.isfile(input_path) and input_path.endswith(('.mp3')):
        # 直接处理单个文件
        print(f"Processing {input_path}")
        print_loudness(input_path)  # 输出当前响度
    else:
        print(f'{input_path} is not a valid audio file or directory.')

def print_loudness(file_path):
    try:
        # 获取当前响度信息
        command = [
            'ffmpeg-normalize',
            file_path,
            '--dry-run',  # 不修改文件，仅获取信息
            '--print',  # 打印响度信息
        ]
        result = subprocess.run(command, capture_output=True, text=True, check=True)
        # 输出响度信息
        print(f"Loudness information for {file_path}:")
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f'Error processing {file_path}: {e}')
    except Exception as e:
        print(f'Unexpected error: {e}')

if __name__ == "__main__":
    input_directory = '../RollGame/assets'  # 替换为你的音频文件目录
    normalize_audio(input_directory)
